package com.example.user.controller;

import com.example.common.response.Result;
import com.example.user.entity.CartItem;
import com.example.user.service.CartService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/cart")
public class CartController {

    @Autowired
    private CartService cartService;

    @GetMapping("/user/{userId}")
    public Result<Map<CartItem, Object>> getCartItemsWithProducts(@PathVariable Long userId) {
        return cartService.getCartItemsWithProducts(userId);
    }

    @GetMapping("/user/{userId}/items")
    public Result<List<CartItem>> getCartItems(@PathVariable Long userId) {
        return cartService.getCartItems(userId);
    }

    @PostMapping("/user/{userId}/product/{productId}")
    public Result<Boolean> addToCart(
            @PathVariable Long userId,
            @PathVariable Long productId,
            @RequestParam Integer quantity) {
        return cartService.addToCart(userId, productId, quantity);
    }

    @PutMapping("/user/{userId}/product/{productId}")
    public Result<Boolean> updateCartItem(
            @PathVariable Long userId,
            @PathVariable Long productId,
            @RequestParam Integer quantity) {
        return cartService.updateCartItem(userId, productId, quantity);
    }

    @DeleteMapping("/user/{userId}/product/{productId}")
    public Result<Boolean> removeFromCart(
            @PathVariable Long userId,
            @PathVariable Long productId) {
        return cartService.removeFromCart(userId, productId);
    }
} 